// pages/selfremind/mainpage/mainpage.js
var dateTimePicker = require('../../utils/dateTimePicker.js');
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: util.formatDate1(new Date),
    remind: [{ 
      selfRemindId: 1,
      selfRemindContent: "创建你的第一条提醒吧!(仅供参考)",
      selfRemindDate: '2019-11-13',
      selfRemindDetail: '',
    }],
    remind1: [{
        selfRemindId: 1,
        selfRemindContent: "明天上午一共有3个预约的患者明天上午一共有3个预约的患者明天上午一共有3个预约的患者明天上午一共有3个预约的患者",
        selfRemindDate: '2021-1-1',
        selfRemindDetail: '',
      }, 
      {
        selfRemindId: 2,
        selfRemindContent: "张医生让我提醒31号的患者明天来..",
        selfRemindDate: '2020-12-30',
        selfRemindDetail: '',
      },
      {
        selfRemindId: 3,
        selfRemindContent: "开会",
        selfRemindDate: '2021-1-2',
        selfRemindDetail: '',
      }
    ],
    scrollHeight: 0,
    scl: 0,
    currentId: -1,
    //修改/增加

    //自我提醒定时器
    setInter: '',
    num: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRemind()
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 80) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getRemind()
    var that = this
    try {
      var value = wx.getStorageSync('selfRemind')
      if (value) {
        that.setData({
          remind: value,
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 添加
   */
  addSelfRemind: function(e) {
    wx.navigateTo({
      url: '../setRemind/setRemind?newOrModi=0&&queryBean=null',
    })
  },

  /**
   * 修改
   */
  modifySelfRemind: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var queryBean = JSON.stringify(that.data.remind[index])
    wx.navigateTo({
      url: '../setRemind/setRemind?newOrModi=1&&queryBean=' + queryBean,
    })
  },

  /**
   * 删除
   */
  deleteSelfRemind: function(e) {
    var that = this
    var id = e.currentTarget.dataset.idx
    var arr = that.data.remind
    var remindId = arr[id].selfRemindId
    var url = getApp().globalData.url
    wx.showModal({
      title: '删除提醒',
      content: '确定要删除该提醒？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: 'skyblue', //确定文字的颜色
      success: function(res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } 
        else {
          wx.request({
            url: url + 'deleteSelfRemind',
            method: 'POST',
            data: {
              selfRemindId: remindId
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success(res) {
              if (res.data != false && res.data != "false" && res.data!="") {
                that.getRemind()
              }
            },
            fail(res) {
              wx.showToast({
                title: '删除失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  },

  /**
   * 获取备忘录
   */
  getRemind: function(){
    var that = this
    var url = getApp().globalData.url
    var id = getApp().globalData.username
    wx.request({
      url: url + 'getRemindServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        assistantTel: id
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data != "空") {
          try {
            wx.setStorageSync("remind", res.data)
          } catch (e) { }
          that.setData({
            remind: res.data
          })
        }
        else if (res.data == "空") {
          that.setData({
            remind: [{ 
              selfRemindId: 1,
              selfRemindContent: "创建你的第一条提醒吧!(仅供参考)",
              selfRemindDate: '2019-11-13',
              selfRemindDetail: '',
            }]
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})